<template>
    <div class="">
        <!-- 条件筛选-----查询 -->
        <el-form :inline="true">
            <div style="display: flex; justify-content: space-between;">
                <div>
                    <el-form-item>
                        <el-input v-model="key" clearable placeholder="请输入查询内容" style="width: 400px;" />
                    </el-form-item>
                    <el-form-item>
                        <label>交易类型：</label>
                        <el-select v-model="tradetype" placeholder="币种">
                            <el-option v-for="item in Object.keys(tradetypeList)" :label="tradetypeList[item]"
                                :value="Number(item)" />
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <label>币种：</label>
                        <el-select v-model="coinid" placeholder="币种">
                            <el-option label="全部" :value="0" />
                            <el-option v-for="item in Object.keys(coinList)" :label="coinList[item]"
                                :value="Number(item)" />
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-date-picker v-model="dateTime" type="daterange" range-separator="至"
                            value-format="yyyy-MM-dd" start-placeholder="开始日期" end-placeholder="结束日期" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSearch">查询</el-button>
                    </el-form-item>
                </div>
                <div />
            </div>

            <el-form-item>
                <div class="flex">
                    <div style="font-size: 16px;font-weight: bold;">汇总:</div>
                    <div style="margin-left: 20px;" v-if="total.USDT">【 USDT：{{ total.USDT }} 】</div>
                    <div style="margin-left: 20px;" v-if="total.ALEO">【 ALEO：{{ total.ALEO }} 】</div>
                    <div style="margin-left: 20px;" v-if="total.AL1">【 AL1：{{ total.AL1 }} 】</div>
                    <div style="margin-left: 20px;" v-if="total.AL2">【 AL2：{{ total.AL2 }} 】</div>
                </div>
            </el-form-item>
        </el-form>
        <!-- 列表数据 -->
        <el-table v-loading="listLoading" :data="tableData" border style="width: 100%">
            <el-table-column prop="idx" label="ID" align="center" width="80" />
            <el-table-column prop="username" label="用户名" align="center" />
            <el-table-column prop="days" label="发放时间" align="center">
                <template slot-scope="{row}">
                    <div>{{ row.days.replace('T', ' ') }}</div>
                </template>
            </el-table-column>
            <el-table-column prop="tradeType" label="交易类型" align="center">
                <template slot-scope="{row}">
                    <div>{{ tradetypeList[row.tradeType] }}</div>
                </template>
            </el-table-column>
            <el-table-column prop="creditCoinid" label="奖励币种" align="center">
                <template slot-scope="{row}">
                    <div>{{ coinList[row.creditCoinid] }}</div>
                </template>
            </el-table-column>
            <el-table-column prop="creditAmount" label="奖励/收益金额" align="center" />
            <el-table-column prop="sendState" label="状态" align="center">
                <template slot-scope="{row}">
                    <div v-if="row.sendState == 1">待发放</div>
                    <div v-if="row.sendState == 2">已发放</div>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button size="mini" type="primary"
                        @click="$router.push({ name: 'RecordIncome', query: { ctime: scope.row.days, tradeType: scope.row.tradeType, coinid: scope.row.creditCoinid, key: scope.row.username } })">明细</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="block " style="padding-top: 20px;">
            <el-pagination background :current-page="pageIndex" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="totalCount"
                @size-change="idx => { pageSize = idx }" @current-change="idx => { pageIndex = idx }" />
        </div>
    </div>
</template>

<script>
import { creditDay } from '@/api/users.js'
import { GetEnum } from "@/api/ctrls.js";
import { formatDate } from '@/utils/index.js'
export default {
    name: 'Name',
    data() {
        return {
            key: '',
            userid: '',
            coinid: 0, // 币种ID
            tradetype: 0, // 交易类型
            dateTime: [], // 时间查询
            stime: '', // 开始时间 2022-01-01
            etime: '', // 结束时间


            listLoading: false,
            tableData: [],
            totalCount: 0,
            pageIndex: 1,
            pageSize: 10,
            total:{},

            coinList: {},
            tradetypeList: {}
        }
    },
    computed: {
        // ...mapState({
        //   tradetypeList: state => state.user.enumList["TradeType"]
        // })
    },
    watch: {
        pageIndex(val) {
            console.log(val)
            this.getData()
        },
        pageSize(val) {
            console.log(val)
            this.getData()
        }
    },
    async created() {
        this.coinid = Number(this.$route.query.coinid) || 0
        this.tradetype = Number(this.$route.query.tradetype) || 0
    },
    async mounted() {
        this.getData()
        this.GetEnum();
    },
    methods: {
        // 获取枚举
        async GetEnum() {
            const res = await GetEnum();
            this.coinList = res.data['Coin'];
            this.tradetypeList = res.data['TradeType'];
        },
        async onSearch() {
            this.PageIndex = 1
            this.getData()
        },
        // 获取数据
        async getData() {
            this.listLoading = true
            const { list, totalCount,total } = await creditDay({
                key: this.key,
                userid: this.userid,
                coinid: this.coinid,
                tradetype: this.tradetype,
                stime: this.dateTime ? this.dateTime[0] : '',
                etime: this.dateTime ? this.dateTime[1] : '',
                pageIndex: this.pageIndex,
                pageSize: this.pageSize
            })
            this.totalCount = totalCount
            this.total = total
            this.tableData = list
            this.listLoading = false
        }
    }
}
</script>

<style lang="scss">
.el-table .warning-row {
    background: oldlace;
}
</style>